package com.smartum.mpr.client.event;


import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;

import java.util.Date;

/**
 * Calls when language drop-down changes it value.
 * <p/>
 * Implements {@link ChangedHandler#onChanged(com.smartgwt.client.widgets.form.fields.events.ChangedEvent)} which set
 * selected language in the <code>gwtLocale</code> cookie, which used then for the gwt localization.
 */
public class LanguageChangeHandler implements ChangedHandler {

    public static final String LANGUAGE_COOKIE = "gwtLocale";

    /**
     * Add selected locale into the <code>gwtLocale</code> cookie.
     *
     * @param selectedLocale
     */
    private void doLanguageSelectionNavigation(String selectedLocale) {
        Date expires = new Date();
        expires.setYear(expires.getYear() + 1);
        Cookies.setCookie(LANGUAGE_COOKIE, selectedLocale, expires);
        Window.Location.reload();
    }

    public void onChanged(ChangedEvent changedEvent) {
        doLanguageSelectionNavigation((String) changedEvent.getValue());
    }
}
